package com.atguigu.bean.mapper.mybatis.crud;

import com.atguigu.bean.mapper.mybatis.bean.User;
import com.atguigu.bean.mapper.mybatis.mapper.UserMapper;
import com.atguigu.bean.mapper.mybatis.utils.SqlsessionUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author wk
 * @create 2021-05-26 9:25
 */
public class CRUDTest {
    @Test
    public  void testSelectBean() {
        SqlSession sqlsession = SqlsessionUtils.getSqlsession();
        UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
        User user = userMapper.selectBean();
        System.out.println(user);
    }

    @Test
    public void testSelectList() {
        SqlSession sqlsession = SqlsessionUtils.getSqlsession();
        UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
        List<User> list = userMapper.selectBeanList();
        list.forEach(user -> System.out.println(user));
    }

    @Test
    public void testUpdate() {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession(true);
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            userMapper.updateUser();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testDelete() {
        try {
            // 读取mybatis的核心配置文件
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            // 获取工厂对象
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory，生产SqlSession对象
            // true表示设置为自动提交事务
            SqlSession sqlSession = sessionFactory.openSession(true);
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            userMapper.deleteUser();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testInsert() {
        try {
            // 1.读取mybatis的核心配置文件
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            // 2.创建SqlSessionFactoryBuilder对象
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            // 3.获取SqlSession对象
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            // 4.面向接口编程，创建接口的对象进而调用接口的方法
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            int i = userMapper.insertUser();
            // 5.事务提交
            sqlSession.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
